 //
//  UITableViewController+BGStatus.m
//  bigMall
//
//  Created by LTOVE on 16/9/9.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "UITableViewController+BGStatus.h"
#import "BGChectDateStatus.h"

static UIView *view;
static UIView *netWorkStatusView;
static BOOL netStatus = YES;
@implementation UITableViewController (BGStatus)

- (void)addView
{
    view = [[UIView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    view.hidden = YES;
    UIImageView *imageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"抱歉~暂无数据"]];
    imageView.center = CGPointMake(view.width * 0.5 , view.height * 0.5);
    [view addSubview:imageView];
    UILabel *statusInfo = [UILabel new];
    statusInfo.text = @"抱歉~暂无数据";
    [statusInfo sizeToFit];
    statusInfo.y = CGRectGetMaxY(imageView.frame) + 10;
    statusInfo.centerX = imageView.centerX;
    [view addSubview:statusInfo];
    
    view.centerX = self.view.centerX;
    view.centerY = self.tableView.centerY * 0.4;
    //    view.backgroundColor = read_;
    
    [self.tableView addSubview:view];
}


- (void)addNetWirkStatusView
{
    netWorkStatusView = [[UIView alloc]initWithFrame:self.tableView.bounds];
    netWorkStatusView.backgroundColor = dockwhite_;
    netWorkStatusView.hidden = YES;
//    [netWorkStatusView becomeFirstResponder];
    [self.view addSubview:netWorkStatusView];
    [self.view bringSubviewToFront:netWorkStatusView];
    //wifi图标
    UIImageView *wifiImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"shape-281"]];
    wifiImageView.centerX = netWorkStatusView.width * 0.5;
    wifiImageView.centerY = 160;
    [netWorkStatusView addSubview:wifiImageView];
    
    UILabel *titleLabel = [UILabel new];
    titleLabel.text = @"网络加载失败,请检查您当前的网络环境!";
    titleLabel.font = Font34;
    [titleLabel sizeToFit];
    titleLabel.centerX = wifiImageView.centerX;
    titleLabel.y = CGRectGetMaxY(wifiImageView.frame) + 55;
    [netWorkStatusView addSubview:titleLabel];
    UILabel *detailLaebl = [UILabel new];
    detailLaebl.text = @"您可以点击按钮重新加载";
    detailLaebl.textColor = [UIColor colorWithOx:@"#999999"];
    detailLaebl.font = Font30;
    [detailLaebl sizeToFit];
    detailLaebl.centerX = titleLabel.centerX;
    detailLaebl.y = CGRectGetMaxY(titleLabel.frame) + 19;
    [netWorkStatusView addSubview:detailLaebl];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"重新加载"forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
    btn.font = [UIFont psFontOfSize:30];
//    [btn sizeToFit];
    btn.size = CGSizeMake(screenW * 0.5, 45);
    btn.layer.cornerRadius = 7;
    btn.layer.masksToBounds = YES;
    [btn setBackgroundImage:[UIImage imageWithColor:lightwhite_ size:btn.size] forState:UIControlStateNormal];
    
//    [btn setImage:[UIImage imageWithColor:lightwhite_ size:btn.size] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(referchBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    btn.centerX = titleLabel.centerX;
    btn.y = CGRectGetMaxY(detailLaebl.frame)+ 42.5;
    [netWorkStatusView addSubview:btn];
//    [self.tableView.header endRefreshing];
    
}

- (void)referchBtnClick
{
    [self.tableView.header beginRefreshing];
    netWorkStatusView.hidden = YES;
    self.tableView.scrollEnabled = YES;
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
   
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(dataStatusNotify:) name:@"dataStatus" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(netWorkStatusNotify:) name:@"netWorkStatus" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadErrorNotify) name:@"loadError" object:nil];
    [self addView];
    [self addNetWirkStatusView];
}

- (void)loadErrorNotify
{
    self.tableView.footer.hidden = YES;
    netWorkStatusView.hidden = NO;
    self.tableView.scrollEnabled = NO;
}

- (void)netWorkStatusNotify:(NSNotification *)notify
{
    NSDictionary *dic = notify.userInfo;
    GWLog(@"%@",dic);
    if ([dic[@"key"] isEqualToString:@"0"]) {
        netWorkStatusView.hidden = NO;
        self.tableView.scrollEnabled = NO;
        netStatus = NO;
    }else{
        netWorkStatusView.hidden = YES;
        self.tableView.scrollEnabled = YES;
        netStatus = YES;
        [self referchBtnClick];
    }
    
}



- (void)dataStatusNotify:(NSNotification *)dataStatus
{
    
    NSDictionary *dic = dataStatus.userInfo;
    GWLog(@"%@",dic);
    if ([dic[@"status"] isEqualToString:@"0"] && ![BGChectDateStatus checkDataStatusWithAction:dic[@"className"]]) {
        view.hidden = NO;
    }else{
        view.hidden = YES;
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(dataStatusNotify:) name:@"dataStatus" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(netWorkStatusNotify:) name:@"netWorkStatus" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadErrorNotify) name:@"loadError" object:nil];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
